﻿@inherits AdminCompontentBase
<AutoHeight Overflow="_entities.Result.Any()" AutoClass="mt-6">
    <HeaderContent>
        <div class="fill rounded-2 d-flex justify-center align-center px-6 py-3 mb-6" style="height:56px">
            <SButton Ripple=false Plain IsActive Class="emphasis2--text h6 primary--text" OnClick="NavigateToSend"><MIcon Size=22 Left>mdi-message</MIcon>@T("Permission.SendMessage")</SButton>
        </div>
        <SSearchPanel @bind-Value="@_queryParam.Filter" Advanced OnEnter="RefreshAsync" OnClearClick="HandleClearAsync">
            <ChildContent>
                <DefaultTitle>@T("Title")</DefaultTitle>
            </ChildContent>
            <AdvancedContent>
                <MCol Md="2">
                    <SSelect Small @bind-Value="@_queryParam.TimeType"
                             Items="@(GetEnumList<MessageTaskTimeTypes>())"
                             Label="@T("DisplayName.MessageTaskTimeType")"
                             ItemText="@(item => T($"DisplayName.MessageTaskTimeType.{item.ToString()}"))"
                             ItemValue="item => item"
                             BackgroundColor="white"
                             Clearable
                             HideDetails="@("auto")"
                             TItem="MessageTaskTimeTypes"
                             TItemValue="MessageTaskTimeTypes"
                             TValue="MessageTaskTimeTypes?"
                             Class="rounded-lg body2"
                             OnClearClick="RefreshAsync"
                             OnSelectedItemUpdate="RefreshAsync">
                    </SSelect>
                </MCol>
                <MCol Class="flex-grow-0">
                    <SDateTimeRangeToolbar StartDateTime="_startTime" EndDateTime="_endTime" OnUpdate="DateRangChangedAsync" ShowQuickChange="false" />
                </MCol>
                   
                <MCol Md="2">
                    <SSelect Small @bind-Value="_queryParam.ChannelId"
                             Items="@_channelItems"
                             Label="@T("DisplayName.Channel")"
                             ItemText="item => item.DisplayName"
                             ItemValue="item => item.Id"
                             BackgroundColor="white"
                             Clearable
                             HideDetails="@("auto")"
                             TItem="ChannelDto"
                             TItemValue="Guid"
                             TValue="Guid?"
                             Class="rounded-lg body2"
                             OnClearClick="RefreshAsync"
                             OnSelectedItemUpdate="RefreshAsync">
                    </SSelect>
                </MCol>
                <MCol Md="2">
                    <SSelect Small @bind-Value="@_queryParam.Status"
                             Items="@(GetEnumList<MessageTaskStatuses>())"
                             Label="@T("DisplayName.MessageTaskStatus")"
                             ItemText="@(item => T($"DisplayName.MessageTaskStatus.{item.ToString()}"))"
                             ItemValue="item => item"
                             BackgroundColor="white"
                             Clearable
                             HideDetails="@("auto")"
                             TItem="MessageTaskStatuses"
                             TItemValue="MessageTaskStatuses"
                             TValue="MessageTaskStatuses?"
                             Class="rounded-lg body2"
                             OnClearClick="RefreshAsync"
                             OnSelectedItemUpdate="RefreshAsync">
                    </SSelect>
                </MCol>
                <MCol Md="2">
                    <SSelect Small @bind-Value="@_queryParam.EntityType"
                             Items="@(GetEnumList<MessageEntityTypes>())"
                             Label="@T("DisplayName.MessageTaskEntityType")"
                             ItemText="@(item => T($"DisplayName.MessageEntityType.{item.ToString()}"))"
                             ItemValue="item => item"
                             BackgroundColor="white"
                             Clearable
                             HideDetails="@("auto")"
                             TItem="MessageEntityTypes"
                             TItemValue="MessageEntityTypes"
                             TValue="MessageEntityTypes?"
                             Class="rounded-lg body2"
                             OnClearClick="RefreshAsync"
                             OnSelectedItemUpdate="RefreshAsync">
                    </SSelect>
                </MCol>
                <MCol Md="1">
                    <BooleanSelect @bind-Value="_queryParam.IsEnabled" Label="@T("DisplayName.MessageTaskIsEnabled")" Style="min-width:85px;" OnSelectedItemUpdate="RefreshAsync" />
                </MCol>
            </AdvancedContent>
            <ButtonContent>
                    <MTooltip Bottom>
                    <ActivatorContent>
                        <div class="ml-6 d-flex button fill" @onclick="HandleIsDraft" @attributes="@context.Attrs">
                            <SIcon Class="ma-auto" Color="@(_queryParam.IsDraft==true?"primary":"emphasis2")" Size=20>
                                mdi-file
                            </SIcon>
                        </div>
                    </ActivatorContent>
                    <ChildContent>
                        <span>@(_queryParam.IsDraft == true ? T("All") : T("Draft"))</span>
                    </ChildContent>
                </MTooltip>
            </ButtonContent>
        </SSearchPanel>
    </HeaderContent>
    <AutoHeightContent>
        @if (_entities.Result.Any())
        {
           <div class="grid-card">
                @foreach (var _entitie in _entities.Result)
                {
                    <div class="item">
                        <MessageTaskCard MessageTask="_entitie" OnEdit="()=>HandleEditAsync(_entitie)" OnView="()=>_detailModal.OpenModalAsync(_entitie)" OnTest="()=>_sendTestModal.OpenModalAsync(_entitie.Id,_entitie.Channel?.Type)" OnDelete="()=>HandleDelAsync(_entitie.Id,_entitie.DisplayName)" OnIsEnabled="()=>HandleIsEnabled(_entitie)" />
                    </div>
                }
            </div> 
        }
        else
        {
            <SEmptyPlaceholder />
        }
        
    </AutoHeightContent>
    <FooterContent>
        <div class="mt-6 fill rounded-2 pa-0">
            <SPagination Page="@_queryParam.Page" PageSize="@_queryParam.PageSize" Total="_entities.Total" PageChanged="HandlePageChanged" PageSizeChanged="HandlePageSizeChanged" />
        </div>
    </FooterContent>
</AutoHeight>

<TemplateMessageEditModal @ref="_templateEditModal" OnOk="HandleOk" />
<OrdinaryMessageEditModal @ref="_ordinaryEditModal" OnOk="HandleOk" />
<MessageTaskDetailModal @ref="_detailModal" OnOk="HandleOk" />
<SendTestMessageModal @ref="_sendTestModal" OnOk="HandleOk" />
